package org.test.once.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.test.once.model.Module;
import org.test.once.utils.DBHelper;

/**
 * 数据存储
 * 
 * @author cennanfang
 * @date 2013-7-22
 * @fileName UserDao.java
 */
public class ModuleDao {

	private static ModuleDao instance;

	private  ModuleDao() {
	}

	/**
	 * 获取此类的一个实例
	 * 
	 * @return
	 */
	public static  ModuleDao getInstance() {
		if (instance == null) {
			instance = new  ModuleDao();
		}
		return instance;
	}

	/**
	 * 添加用户
	 * 
	 * @param user
	 * @throws SQLException
	 */
	public void save( Module module) throws SQLException {
		String sql = "insert into t_module(id,name,pid,url) values(?,?, ?, ?)";

		Connection conn = DBHelper.getConnection();
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setInt(1, module.getId());
		pstmt.setString(2, module.getName());
		pstmt.setInt(3,  module.getPid());
		pstmt.setString(4,module.getUrl());
		pstmt.executeUpdate();

		DBHelper.close(pstmt);
		DBHelper.close(conn);
	}


	/**
	 * 通过Id查询
	 * 
	 * @param id
	 * @throws SQLException
	 */
	public Module findModuleById(int id) throws SQLException {
		String sql = "select * from t_module where id = ?";

		Connection conn = DBHelper.getConnection();
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setInt(1, id);
		ResultSet rs = pstmt.executeQuery();
		Module module = null;
		if (rs.next()) {
			module = new Module();
			module.setId(id);
			module.setName(rs.getString("name"));
			module.setPid(rs.getInt("pid"));
			module.setUrl(rs.getString("url"));
		}

		DBHelper.close(rs);
		DBHelper.close(pstmt);
		DBHelper.close(conn);
		return module;
	}

	/**
	 * 
	 * @param account
	 * @return
	 * @throws SQLException
	 */
	
	/**
	 * 查询用户列表
	 * 
	 * @return
	 * @throws SQLException
	 */
	public List<Module> searchModule() throws SQLException {
		String sql = "select * from t_module where pid=1";
		Connection conn = DBHelper.getConnection();
		PreparedStatement pstmt = conn.prepareStatement(sql);
		ResultSet rs = pstmt.executeQuery();
		ArrayList<Module> modules = new ArrayList<Module>();
		while (rs.next()) {
			 Module module = new Module();
			module.setId(rs.getInt("id"));
			module.setName(rs.getString("name"));
			module.setPid(rs.getInt("pid"));
			//module.setUrl(rs.getString("pid"));
			System.out.println(module.getUrl());
			modules.add(module);
		}
		DBHelper.close(rs);
		DBHelper.close(pstmt);
		DBHelper.close(conn);
		return modules;
	}
	public List<Module> findchildModule(int id) throws SQLException {
		String sql = "select * from t_module where pid=?";
		Connection conn = DBHelper.getConnection();
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setInt(1,id);
		ResultSet rs = pstmt.executeQuery();
		ArrayList<Module> modules = new ArrayList<Module>();
		while (rs.next()) {
			 Module module = new Module();
			module.setId(rs.getInt("id"));
			module.setName(rs.getString("name"));
			module.setPid(rs.getInt("pid"));
			module.setUrl(rs.getString("url"));
			System.out.println(module.getUrl());
			modules.add(module);
		}
		DBHelper.close(rs);
		DBHelper.close(pstmt);
		DBHelper.close(conn);
		return modules;
	}

	/**
	 * 通过id删除用户
	 * 
	 * @param id
	 * @throws SQLException
	 */
	public void deleteModule(int id) throws SQLException {
		String sql = "delete from t_module where id = ?";

		Connection conn = DBHelper.getConnection();
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setInt(1, id);
		pstmt.execute();

		DBHelper.close(pstmt);
		DBHelper.close(conn);
	}

	/**
	 * 更新用户信息
	 * 
	 * @param user
	 * @throws SQLException 
	 */
	public void updateModule(Module module) throws SQLException {
		
		String sql = "update t_module set name = ?, pid = ?, url = ? where id = ?";
		System.out.print("134t");

		Connection conn = DBHelper.getConnection();
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setString(1, module.getName());
		pstmt.setInt(2, module.getPid());
		pstmt.setString(3, module.getUrl());
		pstmt.setInt(4, module.getId());
		pstmt.executeUpdate();
		System.out.println("update");

		DBHelper.close(pstmt);
		DBHelper.close(conn);
	}
	
	public static void main(String []arg){
		
		ModuleDao m=new ModuleDao();
		Module module=new Module();
		module.setPid(0);
		try {
			m.save(module);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}

}
